import { Directive } from 'vue'

export const vDebounce: Directive = {
  mounted(el: HTMLElement, binding) {
    const { value, arg = 'click' } = binding
    let timer: number | null = null
    
    el.addEventListener(arg, (...args) => {
      if (timer) clearTimeout(timer)
      timer = setTimeout(() => {
        value(...args)
      }, 300)
    })
  }
}

// 使用方式: <button v-debounce:click="handleClick">Click</button>